﻿import QtQuick 2.12
import QtQuick.Controls 2.5
import QwtPlotWidget 1.0
import QtQuick.Dialogs 1.3
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    id:root
    width: 800
    height: 700
    title: qsTr("QWT in QML")
    property real seriesResolution:1
    property real number:0
    function addPoint( x, y,isNew=false,seriesType="折线图")
    {
        if(isNew)
        {
            var seriesName = "curve_"+qwtPlot.curveNumber;
            qwtPlot.addPoint(seriesName,x,y, seriesType);
            qwtPlot.changeCurrentCurvel(seriesName);
            model3.append({text:seriesName})
        }
        else{
            qwtPlot.addPoint(x,y);
        }
    }
    ColumnLayout{
        height:root.height
        width:root.width
        RowLayout{
            Layout.fillWidth: true
            GridLayout{
                id:grid1
                Layout.alignment: Qt.AlignTop|Qt.AlignLeft
                columns: 3
                rows: 2
                Button {
                    id:colorButton
                    Layout.preferredWidth: curveSelectComboBox.width
                    text: qsTr("设置颜色")
                    onClicked: {
                        if(model3.count!==0)
                        {
                            colorDialog.color=qwtPlot.curveColor;
                            colorDialog.open();
                        }
                        else{
                            messageDialog.open()
                        }
                    }
                }
                Button {
                    text: qsTr("增加曲线")
                    Layout.alignment: Qt.AlignCenter

                    onClicked: {
                        addSeriesDialog.open();
                    }
                }
                Button {
                    text: qsTr("增加趋势线")
                    Layout.alignment: Qt.AlignLeft

                    onClicked: {
                        trendLineDialog.open();
                    }
                }
                ComboBox {
                    id:curveSelectComboBox
                    textRole: "text"
                    Layout.alignment: Qt.AlignCenter

                    property string noItemsText: "请添加曲线"
                    property string hasItemsText: "请选择曲线"
                    property string userFriendlyText: count > 0 ? model3.get(0).text : noItemsText
                    displayText: userFriendlyText
                    model: model3
                    onCurrentIndexChanged: {
                        if (currentIndex >= 0) { // 判断是否有选中的项
                            curveSelectComboBox.displayText = model3.get(currentIndex).text;
                            qwtPlot.changeCurrentCurvel(model3.get(currentIndex).text);
                        }
                    }
                    onCountChanged: {
                        if(count>0)
                        {
                            curveSelectComboBox.displayText = model3.get(count-1).text;
                        }
                        else{
                            curveSelectComboBox.displayText = noItemsText;
                        }
                    }
                }
                RadioButton {
                    text: "原始"
                    id:radioButton
                    Layout.alignment: Qt.AlignCenter
                    checked: true
                    onClicked: qwtPlot.setLinearScale();
                }
                RadioButton {
                    text: "对数"
                    Layout.alignment: Qt.AlignCenter
                    onClicked: qwtPlot.setLogScale();
                }
            }
            GridLayout{
                Layout.alignment: Qt.AlignCenter
                columns: 2
                rows: 2
                rowSpacing: 0
                TextField{
                    id:settingCurveWidth
                    placeholderText: qsTr("曲线宽度")
                    validator: DoubleValidator { }
                    KeyNavigation.tab: tF3
                    selectByMouse: true
                    onAccepted: {
                        if(settingCurveWidth.displayText!='')
                        {
                            qwtPlot.setCurveWidth(Number(settingCurveWidth.text));
                            settingCurveWidth.text="";
                        }
                    }
                }
                Button{
                    text:"设置宽度"
                    onClicked: {
                        if(settingCurveWidth.displayText!='')
                        {
                            qwtPlot.setCurveWidth(Number(settingCurveWidth.text));
                            settingCurveWidth.text="";
                        }
                    }
                }
                RowLayout{
                    spacing: 0
                    TextField{
                        id:tF3
                        Layout.preferredWidth: settingCurveWidth.width/2
                        placeholderText: qsTr("x")
                        KeyNavigation.tab: tF4
                        DoubleValidator{
                            bottom:0
                        }
                        selectByMouse: true
                    }
                    TextField{
                        id:tF4
                        Layout.preferredWidth: settingCurveWidth.width/2
                        placeholderText: qsTr("y")
                        DoubleValidator{
                            bottom:0
                        }
                        KeyNavigation.tab: settingCurveWidth

                        selectByMouse: true
                        onAccepted: {
                            if(tF3.displayText!==''&&tF4.displayText!==''&&curveSelectComboBox.count>0)
                            {
                                qwtPlot.addPoint(Number(tF3.displayText), Number(tF4.displayText))
                                tF3.text="";
                                tF4.text="";
                                tF3.focus=true;
                            }
                        }
                    }
                }
                Button{
                    text:"增加单点"
                    onClicked: {
                        if(tF3.displayText!==''&&tF4.displayText!==''&&curveSelectComboBox.count>0)
                        {
                            qwtPlot.addPoint(Number(tF3.displayText), Number(tF4.displayText))
                            tF3.text="";
                            tF4.text="";
                            tF3.focus=true;
                        }
                    }
                }
            }
        }
        QwtPlotWidget {
            id: qwtPlot
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.rightMargin: 10
            xMin: 0
            xMax: 50
            yMin: 0
            yMax: 50
            xTickCount: 10
            yTickCount: 10
            axisSize: 12
            showLegend: true
            xAxisName:"Times(ms)"
            yAxisName:"Temperature(℃)"
            m_iPointColor:"black"
        }
    }
    Item{
        MessageDialog {
            id: messageDialog
            title: "警告"
            text: "未添加曲线"
            icon: StandardIcon.Warning
            standardButtons: StandardButton.Ok
        }
        ListModel{
            id:model1
            ListElement{
                text:"FluCurve"
            }
            ListElement{
                text:"MeltCurve"
            }
            ListElement{
                text:"HRM"
            }
        }
        ListModel{
            id:model2
            ListElement{
                text:"linear"
            }
            ListElement{
                text:"log"
            }
        }
        ListModel{
            id:model3
        }
        MySettingDialog{
            id:addSeriesDialog
            x:root.width/2-addSeriesDialog.width/2
            y:root.height/2-addSeriesDialog.height/2
            Connections{
                target:addSeriesDialog.saveButton
                onClicked: {
                    if(addSeriesDialog.xInput.displayText!=''&&addSeriesDialog.yInput.displayText!='')
                    {
                        addPoint(Number(addSeriesDialog.xInput.displayText),Number(addSeriesDialog.yInput.displayText), true, cn1.seriesType);
                        addSeriesDialog.close();
                    }
                    else{
                        saveFailedDialog.open();
                    }
                }
            }
            Connections{
                id:cn1
                target:addSeriesDialog.typeBox
                property string seriesType
                onActivated:{
                    seriesType=addSeriesDialog.typeBox.currentText;
                }
            }
            Connections{
                target:addSeriesDialog.cancelButton
                onClicked: {
                    addSeriesDialog.close();
                }
            }
        }
        MessageDialog {
            id: saveFailedDialog
            text: "数据不能为空"
        }
        ColorDialog {
            id: colorDialog
            onAccepted: {
                // 用户点击 "确定" 按钮时触发
                qwtPlot.setCurveColor(colorDialog.color);
            }
        }
        TrendLineDialog{
            id:trendLineDialog
            x:root.width/2-addSeriesDialog.width/2
            y:root.height/2-addSeriesDialog.height/2
            Connections{
                target:trendLineDialog.typeBox
                onActivated: {
                    trendLineDialog.text1.text=trendLineDialog.model1.get(trendLineDialog.typeBox.currentIndex).type;
                    switch(trendLineDialog.typeBox.currentText)
                    {
                    case "线性":
                        trendLineDialog.bInput.visible=true;
                        trendLineDialog.cInput.visible=false;
                        trendLineDialog.xMin.text=0;
                        break;
                    case "多项式":
                        trendLineDialog.bInput.visible=true;
                        trendLineDialog.cInput.visible=true;
                        trendLineDialog.xMin.text=0;
                        break;
                    case "指数":
                        trendLineDialog.bInput.visible=true;
                        trendLineDialog.cInput.visible=false;
                        trendLineDialog.xMin.text=0;
                        break;
                    case "对数":
                        trendLineDialog.bInput.visible=true;
                        trendLineDialog.cInput.visible=false;
                        trendLineDialog.xMin.text=1;
                        break;
                    }
                }
            }
            Connections{
                target:trendLineDialog.saveButton
                function getFunction(type, x, a, b, c)
                {
                    switch(type)
                    {
                    case "线性":
                        return a*x+b;
                    case "多项式":
                        return a*x*x+b*x+c;
                    case "指数":
                        return a*Math.exp(b*x);

                    case "对数":
                        return a+b*Math.log(x);
                    }
                }
                onClicked:  {
                    if(trendLineDialog.xMin.displayText!==''&&trendLineDialog.xMax.displayText!=='')
                    {
                        for(var i=0;i<=number;i++)
                        {
                            var xMin=+trendLineDialog.xMin.displayText;
                            var xMax=+trendLineDialog.xMax.displayText;
                            var a=+trendLineDialog.aInput.displayText;
                            var b=+trendLineDialog.bInput.displayText+i;
                            var c=+trendLineDialog.cInput.displayText;
                            for(var j=xMin;j<=xMax;j+=seriesResolution)
                            {
                                if(j===xMin)
                                {
                                    addPoint(j, getFunction(trendLineDialog.typeBox.currentText, j, a,b,c),true,cn1.seriesType);
                                }else{
                                    addPoint(j, getFunction(trendLineDialog.typeBox.currentText, j, a,b,c),false,cn1.seriesType);
                                }
                            }
                        }
                        trendLineDialog.close();
                    }
                    else{
                        saveFailedDialog.open();
                    }
                }
            }
            Connections{
                target:trendLineDialog.cancelButton
                onClicked:  {
                    trendLineDialog.close();
                }
            }
        }
    }

}
